package com.fwtai.api;

import com.fwtai.bean.PageFormData;
import com.fwtai.bean.PageInfo;
import com.fwtai.datasource.DaoHandle;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 通知|公告
 * @作者 田应平
 * @版本 v1.0
 * @创建时间 2021年7月20日 10:42:45
 * @QQ号码 444141300
 * @Email service@yinlz.com
 * @官网 <url>http://www.fwtai.com</url>
*/
@Repository
public class ApiBuyDao{

  private final DaoHandle dao;

  public ApiBuyDao(final DaoHandle dao){
    this.dao = dao;
  }

  public List<HashMap<String,Object>> getGoodsType(final ArrayList<Long> list,final int type){
    switch (type){
      case 1://消费者从直营店购买
        return dao.queryForListHashMap("api_buy.getGoodsType1",list);
      case 2://直营店从区级[合伙人]购买;
        return dao.queryForListHashMap("api_buy.getGoodsType2",list);//有返回bottles每件的瓶数
      case 3://区级向总公司购买
        return dao.queryForListHashMap("api_buy.getGoodsType3",list);//有返回bottles每件的瓶数
      default:
        return null;
    }
  }

  public List<HashMap<String,Object>> getActivityGoods(final ArrayList<Long> list,final long activityId,final long storeKid){
    final HashMap<String,Object> map = new HashMap<>(2);
    map.put("activityId",activityId);
    map.put("storeKid",storeKid);
    map.put("list",list);
    return dao.queryForListHashMap("api_buy.getActivityGoods",map);
  }

  @Transactional
  public int addOrder(final HashMap<String,Object> goods_order,final HashMap<String,Object> goods_order_log,final ArrayList<HashMap<String,Object>> listDetail){
    final int rows1 = dao.execute("api_buy.add_goods_order",goods_order);
    final int rows2 = dao.execute("api_buy.add_goods_order_log",goods_order_log);
    final int rows3 = dao.execute("api_buy.add_listDetail",listDetail);
    return rows1 > 0 && rows2 > 0 && rows3 > 0 ? rows1 : 0;
  }

  @Transactional
  public int addMallOrder(final HashMap<String,Object> order_info,final ArrayList<HashMap<String,Object>> order_detail,final HashMap<String,Object> order_log){
    final int rows1 = dao.execute("api_buy.add_mall_order_info",order_info);
    final int rows2 = dao.execute("api_buy.add_mall_order_detail",order_detail);
    final int rows3 = dao.execute("api_buy.add_mall_order_log",order_log);
    final int rows4 = dao.execute("api_buy.abatement_goods_inventory",order_detail);//扣减库存
    return rows1 > 0 && rows2 > 0 && rows3 > 0 && rows4 > 0 ? rows1 : 0;
  }

  @Transactional
  public int addMallOrder(final HashMap<String,Object> order_info,final ArrayList<HashMap<String,Object>> order_detail,final HashMap<String,Object> order_log,final HashMap<String,Object> account_out){
    final int rows = dao.execute("api_buy.abatement_user_account",account_out);//扣减积分
    if(rows > 0){
      final int rows1 = dao.execute("api_buy.add_user_account_out",account_out);//新增支出记录
      final int rows2 = dao.execute("api_buy.add_mall_order_info",order_info);
      final int rows3 = dao.execute("api_buy.add_mall_order_detail",order_detail);
      final int rows4 = dao.execute("api_buy.add_mall_order_log",order_log);
      final int rows5 = dao.execute("api_buy.addAddressIntegral",order_info);//订单的收货地址
      final int rows6 = dao.execute("api_buy.abatement_goods_inventory",order_detail);//扣减库存
      return rows1 > 0 && rows2 > 0 && rows3 > 0 && rows4 > 0 && rows5 > 0 && rows6 > 0 ? rows1 : 0;
    }else{
      return rows;
    }
  }

  public int updateOrderStatus(final Map<String, String> params){
    return dao.execute("api_buy.updatePayOrderStatus",params);
  }

  public List<HashMap<String,Object>> getMyOrderInfo(final Long kid){
    return dao.queryForListHashMap("api_buy.getMyOrderInfo",kid);
  }

  public void updateInventory1(final List<HashMap<String,Object>> list){
    dao.execute("api_buy.updateInventory1",list);
  }

  @Transactional
  public void updateInventory2(final List<HashMap<String,Object>> list){
    dao.execute("api_buy.updateInventory2Area",list);//1.扣减区代理的库存数
    dao.execute("api_buy.updateInventory2Store",list);//2.增加直营门店的库存量
  }

  public void updateInventory3(final List<HashMap<String,Object>> list){
    dao.execute("api_buy.updateInventory3",list);
  }

  public void addStoreGoods(final ArrayList<HashMap<String,Object>> allGoods){
    dao.execute("api_buy.addStoreGoods",allGoods);
  }

  public Long getStoreUser(final Long loginId){
    return dao.queryForLong("api_buy.getStoreUser",loginId);
  }

  //处理消费者和直营门店的一一对一的关系
  public void addStoreUser(final long userId,final long storeId){
    final HashMap<String,Long> map = new HashMap<>(2);
    map.put("userId",userId);
    map.put("storeId",storeId);
    dao.execute("api_buy.addStoreUser",map);
  }

  public Long getMyStoreId(final Long loginId){
    return dao.queryForLong("api_buy.getUserStore",loginId);
  }

  //购买类型为buyer_type=1（消费者购买门店商品）
  public List<HashMap<String,Object>> getBuyGoodsIntegral(final long outTradeNo){
    return dao.queryForListHashMap("api_buy.getGoodsIntegral",outTradeNo);
  }

  //购买类型为buyer_type=2（直营门店向区级进货）,此方式支持多种商品的多件商品(即支持购物车),'门店核销'和'微信支付'计算积分
  public List<HashMap<String,Object>> getGoodsIntegral(final long outTradeNo){
    return dao.queryForListHashMap("api_buy.getGoodsIntegral",outTradeNo);
  }

  //获取商品的积分比例,此方式支持多种商品的多件商品(即支持购物车)
  public List<HashMap<String,Object>> getBuyIntegrals(final long buyId){
    return dao.queryForListHashMap("api_buy.getBuyIntegrals",buyId);
  }

  public List<HashMap<String,Object>> getGoodsReferrerIntegral(final long outTradeNo){
    return dao.queryForListHashMap("api_buy.getGoodsReferrerIntegral",outTradeNo);
  }

  //查询积分购买商品的推荐人积分
  public List<HashMap<String,Object>> getIntegralBuyReferrer(final long outTradeNo){
    return dao.queryForListHashMap("api_buy.getIntegralBuyReferrer",outTradeNo);
  }

  public HashMap<String,Object> getGoodsOrderFee(final long outTradeNo){
    return dao.queryForHashMap("api_buy.getGoodsOrderFee",outTradeNo);
  }

  public void addUserSales(final HashMap<String,Object> account){
    dao.execute("api_buy.addSingleUserAccount",account);
  }

  public void addStoreIntegral(final HashMap<String,Object> params){
    dao.execute("api_buy.addSingleUserAccount",params);
  }

  public List<HashMap<String,Object>> getProxyExist(final Long userId){
    return dao.queryForListHashMap("api_buy.getProxyExist",userId);
  }

  public void addProxyIntegral(final List<HashMap<String,Object>> list){
    dao.execute("api_buy.addUserAccountIn",list);
  }

  public Long getStoreBoss(final Long storeId){
    return dao.queryForLong("api_buy.getStoreBoss",storeId);
  }

  public void addGoodsSecurities(final ArrayList<HashMap<String,Object>> lists){
    dao.execute("api_buy.addGoodsSecurities",lists);
  }

  public void addReferrer(final List<HashMap<String,Object>> list){
    dao.execute("api_buy.addReferrerSource",list);
  }

  public BigDecimal getIntegralTotal(final long loginId){
    return dao.queryForBigDecimal("api_sys_user.getIntegralAccount",loginId);
  }

  public BigDecimal getOutTotal(final long loginId){
    return dao.queryForBigDecimal("api_sys_user.getOutTotal",loginId);
  }

  @Transactional
  public int addIntegralBuy(final HashMap<String,Object> integralBuy,final ArrayList<HashMap<String,Object>> list){
    dao.execute("api_buy.addUserIntegralGoods",list);
    return dao.execute("api_buy.addIntegralBuy",integralBuy);
  }

  @Transactional
  public void updateAccount(final HashMap<String,Object> params){
    dao.queryForBigDecimal("api_buy.subtractUserAccount",params);
    dao.queryForBigDecimal("api_buy.addUserAccountOut",params);
  }

  public PageInfo<HashMap<String,Object>> getUsers(final Long kid){
    return dao.getPageTotal(() -> {
      final Integer total = dao.queryForInteger("api_buy.getUsersTotal",kid);
      final List<HashMap<String,Object>> listData = dao.queryForListHashMap("api_buy.getUsersData",kid);
      final PageInfo<HashMap<String,Object>> pageInfo = new PageInfo<>();
      pageInfo.setTotal(total);
      pageInfo.setData(listData);
      return pageInfo;
    });
  }

  public boolean getActivityRecord(final Long loginId,final Long activityId){
    final HashMap<String,Object> params = new HashMap<>(2);
    params.put("user_id",loginId);
    params.put("activity_id",activityId);
    return dao.queryForLong("api_buy.getActivityRecord",params) != null;
  }

  public HashMap<String, Object> getGoodsPromotionIntegral(final long orderId,final long activityId){
    final HashMap<String,Object> params = new HashMap<>(2);
    params.put("orderId",orderId);
    params.put("activityId",activityId);
    return dao.queryForHashMap("api_buy.getGoodsPromotionIntegral",params);
  }

  public HashMap<String, Object> getActivityInfo(final long kid){
    return dao.queryForHashMap("api_buy.getActivityInfo",kid);
  }

  public List<HashMap<String,Object>> getVipIntegral(final long outTradeNo){
    return dao.queryForListHashMap("api_buy.getVipIntegral",outTradeNo);
  }

  public List<HashMap<String,Object>> getVipOrder(final long outTradeNo){
    return dao.queryForListHashMap("api_buy.getVipOrder",outTradeNo);
  }

  public List<Long> getVipUser(final ArrayList<Long> users){
    return dao.queryListEntity("api_buy.getVipUser",users);
  }

  public Long getStoreUserId(final long kid){
    return dao.queryForLong("api_buy.getStoreUserId",kid);
  }

  public HashMap<String, Object> getOutTradeNoUserId(final long kid){
    return dao.queryForHashMap("api_buy.getOutTradeNoUserId",kid);
  }

  public HashMap<String,Object> getUserProxyAreaLevel(final long userId){
    return dao.queryForHashMap("api_buy.getUserProxyAreaLevel",userId);
  }

  public Long getUserParent(final long loginId){
    return dao.queryForLong("api_buy.getUserParent",loginId);
  }

  public Integer getCargoStatus(final long outTradeNo){
    return dao.queryForInteger("api_buy.getCargoStatus",outTradeNo);
  }

  public List<HashMap<String,Object>> getGoodsPrice(final List<Long> params){
    return dao.queryForListHashMap("api_buy.getGoodsPrice",params);
  }

  public void addAddressWechat(final Long outTradeNo){
    dao.execute("api_buy.addAddressWechat",outTradeNo);
  }

  public int updateMallOrder(final HashMap<String,Object> params){
    return dao.execute("api_buy.updateMallOrder",params);
  }

  public List<HashMap<String,Object>> listDataPage(final PageFormData formData){
    return dao.queryForListHashMap("api_buy.listDataPage",formData);
  }

  public HashMap<String,Object> queryOrderInfo(final long kid){
    return dao.queryForHashMap("api_buy.queryOrderInfo",kid);
  }

  public HashMap<String,Object> getOrderLogistics(final HashMap<String,Long> params){
    return dao.queryForHashMap("api_buy.getOrderLogistics",params);
  }

  public List<HashMap<String,Object>> getListDetails(final long out_trade_no){
    return dao.queryForListHashMap("api_buy.getListDetails",out_trade_no);
  }

  public int edit_affirm_delivery(final HashMap<String,Long> params){
    return dao.execute("api_buy.edit_affirm_delivery",params);
  }

  public List<HashMap<String,Object>> getOrderGoodsInfo(final long out_trade_no){
    return dao.queryForListHashMap("api_buy.getOrderGoodsInfo",out_trade_no);
  }

  public Integer getGoodsCoupon(final ArrayList<HashMap<String,Object>> list){
    return dao.queryForInteger("api_buy.getGoodsCoupon",list);
  }

  public Integer getCoupon(final ArrayList<Long> list){
    return dao.queryForInteger("api_buy.getCoupon",list);
  }
}